package com.example.androidcaculator.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.androidcaculator.R;
import com.example.androidcaculator.utils.Converter;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ConverterBaseActivity extends AppCompatActivity {

    private Button[] btn = new Button[10];
    private Button A, B, C, D, E, F, clear, back;

    private TextView num, hexNum, decNum, octNum, binNum;
    private RadioButton Hex, Dec, Oct, Bin;

    private void initView() {

        btn[0] = findViewById(R.id.base_0);
        btn[1] = findViewById(R.id.base_1);
        btn[2] = findViewById(R.id.base_2);
        btn[3] = findViewById(R.id.base_3);
        btn[4] = findViewById(R.id.base_4);
        btn[5] = findViewById(R.id.base_5);
        btn[6] = findViewById(R.id.base_6);
        btn[7] = findViewById(R.id.base_7);
        btn[8] = findViewById(R.id.base_8);
        btn[9] = findViewById(R.id.base_9);

        A = findViewById(R.id.base_A);
        B = findViewById(R.id.base_B);
        C = findViewById(R.id.base_C);
        D = findViewById(R.id.base_D);
        E = findViewById(R.id.base_E);
        F = findViewById(R.id.base_F);

        clear = findViewById(R.id.base_clear);
        back = findViewById(R.id.base_back);
        num = findViewById(R.id.base_value);
        hexNum = findViewById(R.id.hexTXT);
        decNum = findViewById(R.id.decTXT);
        octNum = findViewById(R.id.octTXT);
        binNum = findViewById(R.id.binTXT);

        Hex = findViewById(R.id.btnHEX);
        Dec = findViewById(R.id.btnDEC);
        Oct = findViewById(R.id.btnOCT);
        Bin = findViewById(R.id.btnBIN);

        //为按钮绑定监听器
        btn[0].setOnClickListener(actionNum);
        btn[1].setOnClickListener(actionNum);
        btn[2].setOnClickListener(actionNum);
        btn[3].setOnClickListener(actionNum);
        btn[4].setOnClickListener(actionNum);
        btn[5].setOnClickListener(actionNum);
        btn[6].setOnClickListener(actionNum);
        btn[7].setOnClickListener(actionNum);
        btn[8].setOnClickListener(actionNum);
        btn[9].setOnClickListener(actionNum);
        A.setOnClickListener(actionNum);
        B.setOnClickListener(actionNum);
        C.setOnClickListener(actionNum);
        D.setOnClickListener(actionNum);
        E.setOnClickListener(actionNum);
        F.setOnClickListener(actionNum);

        clear.setOnClickListener(actionOperation);
        back.setOnClickListener(actionOperation);

        //为单选添加监听器
        Hex.setOnClickListener(actionBase);
        Dec.setOnClickListener(actionBase);
        Oct.setOnClickListener(actionBase);
        Bin.setOnClickListener(actionBase);

    }

    //选择样式时更新
    private void AppearanceShow() {
        if (Dec.isChecked()) {
            A.setBackgroundResource(R.drawable.bt_unchecked);
            B.setBackgroundResource(R.drawable.bt_unchecked);
            C.setBackgroundResource(R.drawable.bt_unchecked);
            D.setBackgroundResource(R.drawable.bt_unchecked);
            E.setBackgroundResource(R.drawable.bt_unchecked);
            F.setBackgroundResource(R.drawable.bt_unchecked);

            A.setEnabled(false);
            B.setEnabled(false);
            C.setEnabled(false);
            D.setEnabled(false);
            E.setEnabled(false);
            F.setEnabled(false);

            for (int i = 0; i < 10; i++) {
                btn[i].setBackgroundResource(R.drawable.bt_number);
                btn[i].setTextColor(Color.parseColor("#000000"));
                btn[i].setEnabled(true);
            }
        }
        if (Oct.isChecked()) {
            A.setBackgroundResource(R.drawable.bt_unchecked);
            B.setBackgroundResource(R.drawable.bt_unchecked);
            C.setBackgroundResource(R.drawable.bt_unchecked);
            D.setBackgroundResource(R.drawable.bt_unchecked);
            E.setBackgroundResource(R.drawable.bt_unchecked);
            F.setBackgroundResource(R.drawable.bt_unchecked);

            A.setEnabled(false);
            B.setEnabled(false);
            C.setEnabled(false);
            D.setEnabled(false);
            E.setEnabled(false);
            F.setEnabled(false);

            btn[8].setBackgroundResource(R.drawable.bt_unchecked);
            btn[9].setBackgroundResource(R.drawable.bt_unchecked);
            btn[8].setEnabled(false);
            btn[9].setEnabled(false);
            btn[8].setTextColor(Color.parseColor("#ffffff"));
            btn[9].setTextColor(Color.parseColor("#ffffff"));

            for (int i = 0; i < 8; i++) {
                btn[i].setBackgroundResource(R.drawable.bt_number);
                btn[i].setTextColor(Color.parseColor("#000000"));
                btn[i].setEnabled(true);
            }
        }
        if (Bin.isChecked()) {
            A.setBackgroundResource(R.drawable.bt_unchecked);
            B.setBackgroundResource(R.drawable.bt_unchecked);
            C.setBackgroundResource(R.drawable.bt_unchecked);
            D.setBackgroundResource(R.drawable.bt_unchecked);
            E.setBackgroundResource(R.drawable.bt_unchecked);
            F.setBackgroundResource(R.drawable.bt_unchecked);

            A.setEnabled(false);
            B.setEnabled(false);
            C.setEnabled(false);
            D.setEnabled(false);
            E.setEnabled(false);
            F.setEnabled(false);

            for (int i = 2; i < 10; i++) {
                btn[i].setBackgroundResource(R.drawable.bt_unchecked);
                btn[i].setTextColor(Color.parseColor("#ffffff"));
                btn[i].setEnabled(false);
            }

        }
        if (Hex.isChecked()) {
            A.setBackgroundResource(R.drawable.bt_feature);
            B.setBackgroundResource(R.drawable.bt_feature);
            C.setBackgroundResource(R.drawable.bt_feature);
            D.setBackgroundResource(R.drawable.bt_feature);
            E.setBackgroundResource(R.drawable.bt_feature);
            F.setBackgroundResource(R.drawable.bt_feature);

            A.setEnabled(true);
            B.setEnabled(true);
            C.setEnabled(true);
            D.setEnabled(true);
            E.setEnabled(true);
            F.setEnabled(true);

            for (int i = 0; i < 10; i++) {
                btn[i].setBackgroundResource(R.drawable.bt_number);
                btn[i].setTextColor(Color.parseColor("#000000"));
                btn[i].setEnabled(true);
            }
        }

    }

    //当用户输入数字改变时更新其他显示区的数字
    private void updateShow(Map<String, String> converted) {
        String checkedNum = new String();
        String setNum = new String();

        if (Dec.isChecked()) {
            setNum = converted.get("Hex");
            hexNum.setText(setNum);

            setNum = converted.get("Oct");
            octNum.setText(setNum);

            setNum = converted.get("Bin");
            binNum.setText(setNum);

            checkedNum = decNum.getText().toString();
        }
        if (Hex.isChecked()) {
            setNum = converted.get("Dec");
            decNum.setText(setNum);

            setNum = converted.get("Oct");
            octNum.setText(setNum);

            setNum = converted.get("Bin");
            binNum.setText(setNum);

            checkedNum = hexNum.getText().toString();
        }
        if (Oct.isChecked()) {
            setNum = converted.get("Hex");
            hexNum.setText(setNum);

            setNum = converted.get("Dec");
            decNum.setText(setNum);

            setNum = converted.get("Bin");
            binNum.setText(setNum);

            checkedNum = octNum.getText().toString();
        }
        if (Bin.isChecked()) {
            setNum = converted.get("Hex");
            hexNum.setText(setNum);

            setNum = converted.get("Oct");
            octNum.setText(setNum);

            setNum = converted.get("Dec");
            decNum.setText(setNum);

            checkedNum = binNum.getText().toString();
        }

        //num.setText(checkedNum);


    }


    private View.OnClickListener actionNum = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String base = new String();
            String context = ((Button) view).getText().toString();
            String value = new String();
            Map<String, String> converted = new HashMap<String, String>();   //保存被转换后的结果以呈现在计算器上

            if (Dec.isChecked()) {
                value = decNum.getText().toString();
                base = "Dec";
                if (value.equals("0"))
                    value = context;
                else
                    value += context;

                decNum.setText(value);
                num.setText(decNum.getText().toString());

            } else if (Hex.isChecked()) {
                value = hexNum.getText().toString();
                base = "Hex";
                if (value.equals("0"))
                    value = context;
                else
                    value += context;
                hexNum.setText(value);
                num.setText(hexNum.getText().toString());

            } else if (Oct.isChecked()) {
                value = octNum.getText().toString();
                base = "Oct";
                if (value.equals("0"))
                    value = context;
                else
                    value += context;
                octNum.setText(value);
                num.setText(octNum.getText().toString());

            } else if (Bin.isChecked()) {
                value = binNum.getText().toString();
                base = "Bin";
                value += context;
                binNum.setText(value);
                num.setText(binNum.getText().toString());


            }
            Converter converter = new Converter();
            converted = converter.BaseConversion(base, value);
            updateShow(converted);


        }
    };

    private View.OnClickListener actionOperation = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String context = ((Button) view).getText().toString();
            String value = num.getText().toString();
            Map<String, String> converted = new HashMap<String, String>();
            Converter converter = new Converter();

            if (context.equals("clear")) {
                num.setText("0");
                decNum.setText("0");
                hexNum.setText("0");
                octNum.setText("0");
                binNum.setText("0");

            }
            if (context.equals("back")) {
                if (value.length() > 1) {
                    value = value.substring(0, value.length() - 1);
                    num.setText(value);

                } else {
                    num.setText("0");
                    value = "0";
                }
                switch(WhichChoose()){
                    case "Dec":
                        decNum.setText(num.getText().toString());
                        break;
                    case "Hex":
                        hexNum.setText(num.getText().toString());
                        break;
                    case "Oct":
                        octNum.setText(num.getText().toString());
                        break;
                    case "Bin":
                        binNum.setText(num.getText().toString());
                        break;

                }
                converted = converter.BaseConversion(WhichChoose(),value);
                updateShow(converted);
            }

        }
    };

    public String WhichChoose() {
        String checked = new String();
        if (Dec.isChecked())
            checked = "Dec";
        if (Hex.isChecked())
            checked = "Hex";
        if (Oct.isChecked())
            checked = "Oct";
        if (Bin.isChecked())
            checked = "Bin";

        return checked;

    }

    private View.OnClickListener actionBase = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AppearanceShow();
            switch(WhichChoose()){
                case "Dec":
                    num.setText(decNum.getText().toString());
                    break;
                case "Hex":
                    num.setText(hexNum.getText().toString());
                    break;
                case "Oct":
                    num.setText(octNum.getText().toString());
                    break;
                case "Bin":
                    num.setText(binNum.getText().toString());
                    break;

            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_converter_base);
        initView();
        AppearanceShow();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.base_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        Intent intent;
        switch (id) {
            case R.id.base_action_common:
                intent = new Intent(ConverterBaseActivity.this, MainActivity.class);
                startActivity(intent);
                break;

            case R.id.base_action_scientific:
                intent = new Intent(ConverterBaseActivity.this, ScientificActivity.class);
                startActivity(intent);
                break;
            case R.id.base_transform_currency:
                intent = new Intent(ConverterBaseActivity.this, ConverterCurrencyActivity.class);
                startActivity(intent);
                break;
            case R.id.base_transform_length:
                intent = new Intent(ConverterBaseActivity.this, ConverterLengthActivity.class);
                startActivity(intent);
                break;
            case R.id.base_transform_volume:
                intent = new Intent(ConverterBaseActivity.this, ConverterVolumeActivity.class);
                startActivity(intent);
                break;
            case R.id.base_action_dateCal:
                intent = new Intent(this, DateActivity.class);
                startActivity(intent);
                break;
            case R.id.base_action_help:
                AlertDialog.Builder helpTxt = new AlertDialog.Builder(ConverterBaseActivity.this);
                helpTxt.setMessage("这里是帮助文档\n本计算器由Dio开发").setTitle("Help");
                helpTxt.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                helpTxt.show();

                break;

            case R.id.base_action_exit:
                intent = new Intent(this, MainActivity.class);
                intent.putExtra(MainActivity.EXIST, true);
                startActivity(intent);


        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        return super.onMenuOpened(featureId, menu);
    }
}